<?php
require "../../sesi_login.php";
require "../../konfigurasi.php";

if(isset($_POST['barangbukti']) && $_POST['barangbukti']=="tambah") {
	koneksi_buka();
	
	$kd_perkara	= $_POST['kd_perkara'];
	$kd_reg_bb	= $_POST['kd_reg_bb'];
	$tanggal	= konversi_tanggal('Y-m-d',$_POST['tanggal']);
	$jenis		= $_POST['jenis'];
	$bb			= $_POST['bb'];
	$satuan		= $_POST['satuan'];
	$jumlah		= $_POST['jumlah'];
	
	mysql_query("INSERT INTO t_bb (kd_perkara, kd_reg_bb, tanggal) VALUES('$kd_perkara','$kd_reg_bb','$tanggal')");
	$idbb= mysql_insert_id();
	//~ $q	= mysql_query("select last_insert_id() as idbb");
	//~ while($dt = mysql_fetch_array($q)) {
			//~ = $dt['idbb'];
	//~ }
	
	mysql_query("insert into t_bb_detail (kd_bb,keterangan,kd_jenis,jumlah,satuan) value('$idbb','$bb','$jenis','$jumlah','$satuan')");	
	header("Location:../../barangbukti.php?suksestambah");
	
	koneksi_tutup();
} elseif(isset($_POST['barangbukti']) && $_POST['barangbukti']=="ubah") {
	koneksi_buka();
	/*$kd_bb 		= $_POST['kd_bb'];
	$kd_perkara	= $_POST['kd_perkara'];
	$kd_reg_bb	= $_POST['kd_reg_bb'];
	$tanggal	= $_POST['tanggal'];*/
	
	$kd_perkara	= $_POST['kd_perkara'];
	$kd_reg_bb	= $_POST['kd_reg_bb'];
	$tanggal	= konversi_tanggal('Y-m-d',$_POST['tanggal']);
	$jenis		= $_POST['jenis'];
	$bb			= $_POST['bb'];
	$kd_bb		= $_POST['kd_bb'];
	$satuan		= $_POST['satuan'];
	$jumlah		= $_POST['jumlah'];
	$kddetail	= $_POST['kddetail'];
		
	mysql_query("
		UPDATE t_bb SET
		kd_perkara = '$kd_perkara',
		kd_reg_bb = '$kd_reg_bb',
		tanggal = '$tanggal'		
		WHERE kd_bb=".$kd_bb
		);
		
	mysql_query(" update t_bb_detail set
			kd_bb='$kd_bb',
			kd_jenis ='$jenis',
			keterangan ='$bb',
			jumlah	='$jumlah',
			satuan	='$satuan'
			where kd_detail='$kddetail'
		");
		
	header("Location:../../barangbukti.php?suksesubah");
	
	koneksi_tutup();
	
}if(isset($_POST['barangbukti']) && $_POST['barangbukti']=="upload_gambar") {
	koneksi_buka();
	
	$kd_bb	= $_POST['kd_bb'];
	
	
	$result = mysql_query("INSERT INTO t_bb_file (kd_bb) VALUES('$kd_bb')");
	if($result){
		$idFoto= mysql_insert_id();		
		$namafolder="../../datafile/"; 
		
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		
		$edit_gbr = $namafolder."BB".$idFoto.".".$extension;
		$path_file = "datafile/"."BB".$idFoto.".".$extension;
		
		if (!move_uploaded_file($_FILES["file"]["tmp_name"], $edit_gbr)) {
			echo 'failed upload';
 		}else{
			$result = mysql_query("UPDATE t_bb_file SET path_file = '$path_file' WHERE id = $idFoto");
			if($result){
				header("Location:../../barangbukti.php?foto=$kd_bb");
			}else{
				echo 'failed_update'."UPDATE t_bb_file SET path_file = '$path_file' WHERE id = $idFoto";
			}
		}				
 		
		
	}else{
		echo 'failed_insert'."INSERT INTO t_bb_file (kd_bb) VALUES('$kd_bb')";
	}
	
	koneksi_tutup();
} elseif(isset($_GET['kd']) && $_GET['kd']!="") {
	koneksi_buka();
	mysql_query("DELETE FROM t_bb WHERE kd_bb='".$_GET['kd']."'");
	header("Location:../../barangbukti.php?sukseshapus");
	koneksi_tutup();
} else {
	header("Location:../../barangbukti.php");
}
?>
